//
//  WXPAuthProgressMoneyView.m
//  进度金额 + 右边的金袋
//
//  Created by wxp on 2025/2/28.
//

#import "WXPAuthProgressMoneyView.h"

@implementation WXPAuthProgressMoneyView

#pragma mark - lazy
- (UIView *)backView
{
    if (_backView == nil) {
        _backView = [UIView  new];
        _backView.backgroundColor = COLOR_LINE;
        _backView.widthHeight = XY((SCREEN_WIDTH-2*20)-(SCREEN_WIDTH-2*20)/(self.moneyAry.count-1), 2);
    }
    return  _backView;
}
- (UIView *)markLineView{
    if (_markLineView == nil) {
        _markLineView = [UIView  new];
        _markLineView.height = self.backView.height;
        _markLineView.backgroundColor = COLOR_LINE;
        _markLineView.hidden = true;
        //_markLineView.backgroundColor = [UIColor colorWithHexString:@"#2ED89F"];
    }
    return  _markLineView;
}
- (NSMutableArray *)moneyAry
{
    if (_moneyAry == nil) {
        _moneyAry = [NSMutableArray arrayWithArray:@[@"$500",@"1000",@"1500",@"2000",@"2500",@"3000",@"???"]];
    }
    return  _moneyAry;
}

#pragma mark - 初始化
- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        self.widthHeight = XY(SCREEN_WIDTH, 0);
        self.backgroundColor = [UIColor clearColor];
        [self addSubView];
    }
    return self;
}

- (void)addSubView{
    [self addSubview:self.backView];
    self.backView.leftTop = XY((20), Ratio(12));
    [self addSubview:self.markLineView];
    self.markLineView.leftCenterY = XY(self.backView.left, self.backView.centerY);
    
    CGFloat viewWidth = Ratio(20);
    CGFloat interval = (SCREEN_WIDTH-2*(20)-(self.moneyAry.count)*viewWidth)/(self.moneyAry.count-1);
    for (int i = 0; i < self.moneyAry.count; i ++) {
        UIImageView * view = [UIImageView new];
        view.tag = i;
        view.widthHeight = XY(viewWidth, viewWidth);
        view.image = [UIImage imageNamed:@"icon-auth-progress-unfinished"];
        view.leftCenterY = XY(self.backView.left+(viewWidth+interval)*i, self.backView.centerY);
        [self addSubview:view];
        
        if(i < (self.moneyAry.count-1)){
            
        }else{
            [GlobalMethod setRoundView:view color:[UIColor clearColor] numRound:0 width:0];
            view.backgroundColor = [UIColor clearColor];
            view.image = [UIImage imageNamed:@"icon-auth-progress-dollar-coin"];
            view.widthHeight = XY(Ratio(26), Ratio(24));
            view.centerXCenterY = XY(view.centerX-Ratio(3), view.centerY-Ratio(2));
        }
        
        
        UILabel * moneyLabel = [UILabel new];
        moneyLabel.fontNum = (11);
        moneyLabel.textColor = [UIColor colorWithHexString:@"565656"];
        moneyLabel.tag = i;
        [moneyLabel fitTitle:@"0" variable:0];
        moneyLabel.centerXTop = XY(view.centerX, view.bottom+(9)-((i==(self.moneyAry.count-1))?Ratio(2):0));
        [self addSubview:moneyLabel];

        if(i == 0){
            self.height = moneyLabel.bottom+(12);
        }
    }
}


#pragma mark - 刷新View
- (void)resetViewWithTag:(NSInteger)tag{
    self.markTag = tag;
    self.markLineView.width = (self.backView.width-(26))/(self.moneyAry.count-2)*(tag-1)+(26);
    if(tag == 0){
        self.markLineView.width = 0;
    }
    [GlobalMethod setRoundView:self.markLineView color:self.markLineView.backgroundColor numRound:self.markLineView.height/2 width:0];
    
    for (UIImageView * view in self.subviews) {
        if([view isKindOfClass:[UIImageView class]]){
            if(view.tag < tag){
                if(view.tag == self.moneyAry.count-1){
                    view.image = [UIImage imageNamed:@"icon-auth-progress-dollar-coin"];
                }else{
                    view.image = [UIImage imageNamed:@"icon-auth-progress-finished"];
                }
            }else{
                if(view.tag == self.moneyAry.count-1){
                    if(tag == self.moneyAry.count-1){
                        view.image = [UIImage imageNamed:@"icon-auth-step-bankcard"];
                    }else{
                        view.image = [UIImage imageNamed:@"icon-auth-progress-dollar-coin"];
                    }
                }
            }
        }
        if([view isKindOfClass:[UILabel class]] && view.tag < tag){
            UILabel * label = (UILabel *)view;
            label.backgroundColor = [UIColor clearColor];
            label.textColor = COLOR_BLACK;
        }
    }
}

- (void)resetViewWithAnimation{
    [UIView animateWithDuration:1.2 animations:^{
        NSInteger lastTag = (self.markTag==self.moneyAry.count-1)?self.markTag-1:self.markTag;
        self.markLineView.width = (self.backView.width-Ratio(20))/(self.moneyAry.count-2)*(lastTag)+(self.markTag>0?(26):0);
        [GlobalMethod setRoundView:self.markLineView color:self.markLineView.backgroundColor numRound:self.markLineView.height/2 width:0];
        
    } completion:^(BOOL finished) {
        for (UIImageView * view in self.subviews) {
            if([view isKindOfClass:[UIImageView class]] && view.tag <= self.markTag){
                if(view.tag == self.moneyAry.count-1){
                    if(view.tag == self.moneyAry.count-1){
                        if(self.markTag == self.moneyAry.count-1){
                            view.image = [UIImage imageNamed:@"icon-auth-step-bankcard"];
                        }else{
                            view.image = [UIImage imageNamed:@"icon-auth-progress-dollar-coin"];
                        }
                    }
                }else{
                    view.image = [UIImage imageNamed:@"icon-auth-progress-finished"];
                }
                
            }
            if([view isKindOfClass:[UILabel class]] && view.tag <= self.markTag){
                UILabel * label = (UILabel *)view;
                label.backgroundColor = [UIColor clearColor];
                label.textColor = COLOR_BLACK;
            }
        }
    }];
}

- (void)resetViewWithMoney:(NSMutableArray *)array{
    for (UIImageView * view in self.subviews) {
        if([view isKindOfClass:[UILabel class]]){
            UILabel * label = (UILabel *)view;
            NSString * money = array[label.tag];
            [label fitTitle:[NSString stringWithFormat:@"%@%@",label.tag==0?@"$":@"",money] variable:0];
            CGFloat viewWidth = (26);
            CGFloat interval = (SCREEN_WIDTH-2*(20)-(self.moneyAry.count)*viewWidth)/6;
            label.centerX = self.backView.left+(viewWidth+interval)*label.tag+viewWidth/2;
        }
    }
}

@end
